<!-- eslint-disable prettier/prettier -->
<!-- 用户编辑弹窗 -->
<template>
	<ele-modal
		:width="750"
		:visible="visible"
		:confirm-loading="loading"
		:title="actionStatus == 'edit' ? '修改分配原因' : '新建分配原因'"
		:body-style="{ paddingBottom: '8px' }"
		@update:visible="updateVisible"
		@ok="save"
	>
		<a-form :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }" :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }">
			<a-row :gutter="24">
				<a-col :md="23" :sm="23" :xs="23">
					<a-form-item label="分配原因" v-bind="validateInfos.content">
					  <a-textarea
					    allow-clear
					    placeholder="请输入"
					    v-model:value="form.content"
					    autocomplete="off"
					  />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import { addreasons, editreasons } from '@/api/jewelry/receipts_return';

  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String,
    // 编辑数据id
    reason: Object
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  const form = reactive({
    content: '',
    receipts_type: 5
  });
  form.id = props.reason.id;
  form.content = props.reason.content;

  // 表单验证规则
  const rules = reactive({
    content: [
      {
        required: true,
        message: '请输入原因',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    console.log('form', form);
    validate()
      .then(() => {
        if (form.id) {
          editreasons(form)
            .then((res) => {
              emit('done');
              updateVisible(false);
              message.success(res.msg);
            })
            .catch((e) => {
              message.error(e);
            });
        } else {
          addreasons(form)
            .then((res) => {
              emit('done');
              updateVisible(false);
              message.success(res.msg);
            })
            .catch((e) => {
              message.error(e);
            });
        }
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
